<template>
  <div class="login-page">
    <div class="login-card">
      <a-form
        :model="formState"
        name="basic"
        autocomplete="off"
        @finish="onFinish"
        class="user-layout-login"
      >
        <a-form-item
          name="username"
          :rules="[{ required: true, message: '请输入用户名!' }]"
        >
          <a-input
            v-model:value="formState.username"
            size="large"
            type="text"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <UserOutlined class="site-form-item-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          name="password"
          :rules="[{ required: true, message: '请输入密码!' }]"
        >
          <a-input-password
            v-model:value="formState.password"
            size="large"
            placeholder="请输入密码！"
          >
            <template #prefix>
              <LockOutlined class="site-form-item-icon" />
            </template>
          </a-input-password>
        </a-form-item>
        <a-form-item style="margin-top: 24px">
          <a-button
            size="large"
            type="primary"
            htmlType="submit"
            class="login-button"
            :loading="formState.loading"
            :disabled="formState.loading"
            >登录</a-button
          >
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script setup>
import { userLogin } from '@/api/user'
import { reactive } from 'vue'
import { store } from '@/store'
import router from '@/router'
import { message } from 'ant-design-vue'
const formState = reactive({
  username: '',
  password: '',
  loading: false,
})

const onFinish = async (values) => {
  const { data, code } = await userLogin(values)
  if (code !== 300) return
  store.commit('app/SET_TOKEN', data.token)
  store.commit('app/SET_USERINFO', data.user)
  if (data.user.role === '9') {
    setTimeout(() => router.replace({ path: '/home' }), 1200)
  } else {
    setTimeout(() => router.replace({ path: '/send' }), 1200)
  }
}
</script>

<style scoped lang="less">
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
